package cn.iocoder.yudao.module.system.controller.admin.fieldsetting;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.fieldsetting.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.fieldsetting.FieldSettingDO;
import cn.iocoder.yudao.module.system.service.fieldsetting.FieldSettingService;

@Tag(name = "管理后台 - 列表显示设置")
@RestController
@RequestMapping("/system/field-setting")
@Validated
public class FieldSettingController {

    @Resource
    private FieldSettingService fieldSettingService;

    @PostMapping("/create")
    @Operation(summary = "创建列表显示设置")
    @PreAuthorize("@ss.hasPermission('system:field-setting:create')")
    public CommonResult<Integer> createFieldSetting(@Valid @RequestBody FieldSettingSaveReqVO createReqVO) {
        return success(fieldSettingService.createFieldSetting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新列表显示设置")
    @PreAuthorize("@ss.hasPermission('system:field-setting:update')")
    public CommonResult<Boolean> updateFieldSetting(@Valid @RequestBody FieldSettingSaveReqVO updateReqVO) {
        fieldSettingService.updateFieldSetting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除列表显示设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:field-setting:delete')")
    public CommonResult<Boolean> deleteFieldSetting(@RequestParam("id") Integer id) {
        fieldSettingService.deleteFieldSetting(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除列表显示设置")
                @PreAuthorize("@ss.hasPermission('system:field-setting:delete')")
    public CommonResult<Boolean> deleteFieldSettingList(@RequestParam("ids") List<Integer> ids) {
        fieldSettingService.deleteFieldSettingListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得列表显示设置")
    @Parameter(name = "type", description = "类型", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:field-setting:query')")
    public CommonResult<FieldSettingRespVO> getFieldSetting(Integer type) {
        FieldSettingDO fieldSetting = fieldSettingService.getFieldSetting(type);
        return success(BeanUtils.toBean(fieldSetting, FieldSettingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得列表显示设置分页")
    @PreAuthorize("@ss.hasPermission('system:field-setting:query')")
    public CommonResult<PageResult<FieldSettingRespVO>> getFieldSettingPage(@Valid FieldSettingPageReqVO pageReqVO) {
        PageResult<FieldSettingDO> pageResult = fieldSettingService.getFieldSettingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FieldSettingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出列表显示设置 Excel")
    @PreAuthorize("@ss.hasPermission('system:field-setting:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFieldSettingExcel(@Valid FieldSettingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FieldSettingDO> list = fieldSettingService.getFieldSettingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "列表显示设置.xls", "数据", FieldSettingRespVO.class,
                        BeanUtils.toBean(list, FieldSettingRespVO.class));
    }

}
